// 引入 mock.js
import Mock from 'mockjs';
import article from './json/article.json';
import tag from './json/tag.json';
import fish from './json/fish.json';
import { param2Obj } from '@/utils/index.js';

//Mock.mock  接口，请求方式，返回数据（数据就是json文件夹中的文件自行添加）
Mock.mock(/\/dev-api\/article\/list/, 'get', (options) => {
  let pageNum = param2Obj(options.url).pageNum;
  let pageSize = param2Obj(options.url).pageSize;
  let start = (pageNum - 1) * pageSize;
  let end = pageNum * pageSize;
  return {
    list: article.list.slice(start, end),
    total: article.list.length,
  };
});
Mock.mock('/dev-api/tags/list', 'get', tag);
Mock.mock(/\/dev-api\/collectionWorks\/list/, 'get', (options) => {
  let pageNum = param2Obj(options.url).pageNum;
  let pageSize = param2Obj(options.url).pageSize;
  let start = (pageNum - 1) * pageSize;
  let end = pageNum * pageSize;
  let list = article.list.filter((J) => J.type == 'CollectionWorks');
  return {
    list: list.slice(start, end),
    total: list.length,
  };
});
Mock.mock(/\/dev-api\/tags\/detail/, 'get', (options) => {
  let pageNum = param2Obj(options.url).pageNum;
  let pageSize = param2Obj(options.url).pageSize;
  let name = param2Obj(options.url).name;
  let start = (pageNum - 1) * pageSize;
  let end = pageNum * pageSize;
  let list = article.list.filter((J) => {
    return J.tag.indexOf(name) != -1;
  });
  return {
    list: list.slice(start, end),
    total: list.length,
  };
});
Mock.mock(/\/dev-api\/studyNotes\/list/, 'get', (options) => {
  let pageNum = param2Obj(options.url).pageNum;
  let pageSize = param2Obj(options.url).pageSize;
  let start = (pageNum - 1) * pageSize;
  let end = pageNum * pageSize;
  let list = article.list.filter((J) => J.type == 'StudyNotes');
  return {
    list: list.slice(start, end),
    total: list.length,
  };
});
Mock.mock('/dev-api/article/detail', 'post', (options) => {
  let form = JSON.parse(options.body);
  let dataList = article.list.filter((J) => J.route == form.route);
  return {
    data: dataList[0],
  };
});

Mock.mock('/dev-api/fish/list', 'get', fish);

Mock.mock('/dev-api/article/scrollList', 'post', (options) => {
  let form = JSON.parse(options.body);
  let res = [];
  for (let index = 0; index < form.size; index++) {
    let list = Mock.mock({
      'id|+1': '@increment',
      data: "@date('yyyy-MM-dd')",
      ip: '@ip',
      url: "@url('http')",
    });
    res.push(list);
  }
  return {
    code: 200,
    data: res,
    msg: '操作成功',
  };
});
